const mongoose = require('mongoose')

// 定义模型
const roleItemSchema = new mongoose.Schema({
  role_id: { type: Number, required: true },
  role_name: { type: String, required: true },
  router_ids: { type: Array, required: true },
  router_desc: { type: Array, required: true },
  role_desc: { type: String, required: true }
})

const RoleItem = mongoose.model('role', roleItemSchema)

// 连接数据库
async function connectToDatabase() {
  try {
    await mongoose.connect('mongodb://localhost:27017/account', {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
    console.log('MongoDB connected!')
  } catch (error) {
    console.error('MongoDB connection error:', error)
  }
}

// 插入数据
async function insertMenuItems(menuItems) {
  try {
    const result = await RoleItem.insertMany(menuItems)
    console.log('Data inserted:', result)
  } catch (error) {
    console.error('Error inserting data:', error)
  }
}

// 主程序
async function main() {
  await connectToDatabase()

  const menuItems = [
    {
      role_id: 101,
      role_name: '超级管理员',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'leaflet_wms', 'openlayers_wms', 'cesium_wms', 'webgl_wms', 'wmts', 'leaflet_wmts', 'openlayers_wmts', 'cesium_wmts', 'webgl_wmts', '解析能力文档', 'wms_tool', 'wmts_tool', '用户管理', '个人中心', '关于'],
      role_desc: '超级管理员, 具有所有路由权限'
    },
    {
      role_id: 102,
      role_name: '超级用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'leaflet_wms', 'openlayers_wms', 'cesium_wms', 'webgl_wms', 'wmts', 'leaflet_wmts', 'openlayers_wmts', 'cesium_wmts', 'webgl_wmts', '解析能力文档', 'wms_tool', 'wmts_tool', '个人中心', '关于'],
      role_desc: '超级用户, 具有除用户管理外的其他所有路由权限'
    },
    {
      role_id: 201,
      role_name: 'WMS用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 17, 18, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'leaflet_wms', 'openlayers_wms', 'cesium_wms', 'webgl_wms', '解析能力文档', 'wms_tool', '个人中心', '关于'],
      role_desc: 'WMS用户, 只能访问加载WMS的路由以及文档路由'
    },
    {
      role_id: 202,
      role_name: 'WMTS用户',
      router_ids: [1, 2, 3, 4, 5, 6, 12, 13, 14, 15, 16, 17, 19, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wmts', 'leaflet_wmts', 'openlayers_wmts', 'cesium_wmts', 'webgl_wmts', '解析能力文档', 'wmts_tool', '个人中心', '关于'],
      role_desc: 'WMTS用户, 只能访问加载WMTS的路由以及文档路由'
    },
    {
      role_id: 301,
      role_name: 'leaflet用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'leaflet_wms', 'wmts', 'leaflet_wmts', '个人中心', '关于'],
      role_desc: '只能访问leaflet路由以及文档路由'
    },
    {
      role_id: 302,
      role_name: 'openlayers用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 9, 12, 14, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'openlayers_wms', 'wmts', 'openlayers_wmts', '个人中心', '关于'],
      role_desc: '只能访问openlayers路由以及文档路由'
    },
    {
      role_id: 303,
      role_name: 'cesium用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 10, 12, 15, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'cesium_wms', 'wmts', 'cesium_wmts', '个人中心', '关于'],
      role_desc: '只能访问cesium路由以及文档路由'
    },
    {
      role_id: 304,
      role_name: 'webgl用户',
      router_ids: [1, 2, 3, 4, 5, 6, 7, 11, 12, 16, 21, 22],
      router_desc: ['documents', 'documentsWMTS', 'documentsWMS', 'documents_iClient', 'documents_iClient3D', 'documents_CesiumJS', 'wms', 'webgl_wms', 'wmts', 'webgl_wmts', '个人中心', '关于'],
      role_desc: '只能访问webgl路由以及文档路由'
    }
  ]

  await insertMenuItems(menuItems)

  mongoose.connection.close() // 关闭连接
}

main()
